package cn.lyh.gulimall.product.service;

import cn.lyh.gulimall.product.dao.PmsCategoryMapper;
import cn.lyh.gulimall.product.entity.PmsCategory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lyh
 * @version 1.0
 * @since 2021/4/6
 */
@Service
public class PmsCategoryService extends ServiceImpl<PmsCategoryMapper, PmsCategory> {

	@Autowired
	PmsCategoryBrandRelationService categoryBrandRelationService;


	/**
	 * 查询所有的分类数据，并组装成树形结构
	 *
	 * @return 查询到的数据
	 */
	public List<PmsCategory> listWithTree() {
		// 1.查出所有分类
		List<PmsCategory> pmsCategories = baseMapper.selectList(null);
		// 2.组装成父子的树形结构

		//2.1 找到所有的一级分类
		List<PmsCategory> level1Menus = pmsCategories.stream()
				.filter(pmsCategory -> pmsCategory.getParentCid() == 0)
				.map((menu) -> {
					menu.setChildren(getChildrens(menu, pmsCategories));
					return menu;
				}).sorted((menu1, menu2) -> {
					return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
				})
				.collect(Collectors.toList());

		return level1Menus;
	}

	/**
	 * 递归查找所有菜单的子菜单
	 *
	 * @param root 根目录节点数据
	 * @param all  全部的菜单数据
	 * @return 找到的子菜单
	 */
	public List<PmsCategory> getChildrens(PmsCategory root, List<PmsCategory> all) {
		List<PmsCategory> collect = all.stream().filter(pmsCategory -> {
			return pmsCategory.getParentCid().equals(root.getCatId());
		}).peek(pmsCategory -> {
			//1.找到子菜单
			pmsCategory.setChildren(getChildrens(pmsCategory, all));
		}).sorted((menu1, menu2) -> {
			//2.菜单的排序
			return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
		}).collect(Collectors.toList());
		return collect;
	}

	/**
	 * 批量删除数据库中的menu 逻辑删除方式
	 *
	 * @param asList 前端传入的catid数组
	 */
	public void removeMenuByIds(List<Long> asList) {
		//TODO 1. 删除前要检查当前的菜单是否被其他的地方引用
		baseMapper.deleteBatchIds(asList);
	}

	/**
	 * 找到catelogId的完整路径
	 * [2,25,225]
	 *
	 * @param catelogId id
	 * @return 完整路径
	 */
	public Long[] findCatelogPath(Long catelogId) {
		List<Long> paths = new ArrayList<>();
		List<Long> parentPath = findParentPath(catelogId, paths);
		Collections.reverse(parentPath);
		return parentPath.toArray(new Long[parentPath.size()]);
	}

	private List<Long> findParentPath(Long catelogId, List<Long> paths) {
		//1.查询父类Id并收集
		// 收集当前节点ID
		paths.add(catelogId);
		PmsCategory byId = this.getById(catelogId);
		if (byId.getParentCid() != 0) {
			findParentPath(byId.getParentCid(), paths);
		}
		return paths;
	}

	/**
	 * 级联更新所有关联的数据
	 *
	 * @param pmsCategory
	 */
	@Transactional(rollbackFor = Exception.class)
	public void updateCascade(PmsCategory pmsCategory) {
		this.updateById(pmsCategory);
		categoryBrandRelationService.updateCategory(pmsCategory.getCatId(), pmsCategory.getName());
	}
}
